package com.qgqaqgqa.deviceinfomanager.activity;

import android.content.Intent;
import android.os.Environment;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qgqaqgqa.deviceinfomanager.R;
import com.qgqaqgqa.deviceinfomanager.adapter.DeviceInfoAdapter;
import com.qgqaqgqa.deviceinfomanager.adapter.FileAdapter;
import com.qgqaqgqa.deviceinfomanager.base.BaseProtocolActivity;
import com.qgqaqgqa.deviceinfomanager.intf.OnRecyclerViewItemClickListener;
import com.qgqaqgqa.deviceinfomanager.util.utilcode.util.SizeUtils;
import com.qgqaqgqa.deviceinfomanager.view.CommonTitleBar;
import com.qgqaqgqa.deviceinfomanager.view.LeftSpaceItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.io.File;
import java.io.FileFilter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;

/**
 * 文件管理器
 * User: Created by 钱昱凯
 * Date: 2019/12/23
 * Time: 20:20
 * EMail: 342744291@qq.com
 */
public class FileManagerActivity extends BaseProtocolActivity {

    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    private List<Object> list=new ArrayList<>();
    private FileAdapter adapter;
    private String rootPath;

    public FileManagerActivity() {
        super(R.layout.act_file);
    }

    @Override
    public void initViews() {
        mTitle=new CommonTitleBar(this);
        mTitle.setTitle("文件管理");
        mTitle.showLeftIcon();
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new FileAdapter(this, list);
        mRecyclerview.addItemDecoration(new LeftSpaceItemDecoration(this, SizeUtils.dp2px(1)));
        mRecyclerview.setAdapter(adapter);
        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onRecyclerViewItemClick(View view, int position) {
                if(list.get(position) instanceof String){
                    getList((String)list.get(position));
                }else if(list.get(position) instanceof File){
                    File f=(File)list.get(position);
                    if(f.isFile()){
                        Intent intent=new Intent();
                        intent.putExtra("data",f.getAbsolutePath());
                        setResult(RESULT_OK,intent);
                        finish();
                    }else{
                        getList(f.getAbsolutePath());
                    }
                }
            }
        });
        rootPath=Environment.getExternalStorageDirectory().getAbsolutePath();
        getList(rootPath);
    }

    private void getList(String filePath) {
        list.clear();
        File rf=new File(filePath);
        if(rootPath.equals(filePath)){
            list.add(rf.getParentFile().getAbsolutePath());
        }
        File[] files=rf.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file) {
                return !file.getName().startsWith("\\.");
            }
        });
        List<File> fileList = Arrays.asList(files);
        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if (o1.isDirectory() && o2.isFile())
                    return -1;
                if (o1.isFile() && o2.isDirectory())
                    return 1;
                return o1.lastModified()<o2.lastModified()?1:-1;
//                return o1.getName().compareTo(o2.getName());
            }
        });
        for (File f:fileList)
        list.add(f);
        adapter.notifyDataSetChanged();
    }
}
